home *** CD-ROM | disk | FTP | other *** search
- // -------- strings.c++
-
- #include "parody.h"
-
- // ------- put a string into a pString
- void pString::putstr(char *s)
- {
- delete sptr;
- length = strlen(s);
- sptr = new char[length+1];
- strcpy(sptr, s);
- }
-
- // --- convert from char *
- pString::pString(char *s)
- {
- sptr = NULL;
- putstr(s);
- }
-
- // ------- copy constructor
- pString::pString(pString& s)
- {
- sptr = NULL;
- putstr(s.sptr);
- }
-
- // -------- construct with a size and fill character
- pString::pString(int len, char fill)
- {
- length = len;
- sptr = new char[length+1];
- memset(sptr, fill, length);
- *(sptr+len) = '\0';
- }
-
- // -------- assignment
- pString& pString::operator=(pString &s)
- {
- if (this != &s)
- putstr(s.sptr);
- return *this;
- }
-
- // ------- concatenation operator (str1 + str2)
- pString pString::operator+(pString &s)
- {
- pString temp(strlen(s.sptr) + strlen(sptr));
- strcpy(temp.sptr, sptr);
- strcat(temp.sptr, s.sptr);
- return temp;
- }
-
- // ------ substring: right len chars
- pString pString::right(int len)
- {
- pString tmp(sptr + strlen(sptr) - len);
- return tmp;
- }
-
- // ------ substring: left len chars
- pString pString::left(int len)
- {
- pString tmp(len);
- strncpy(tmp.sptr, sptr, len);
- return tmp;
- }
-
- // ------ substring: middle len chars starting from where
- pString pString::mid(int len, int where)
- {
- pString tmp(len);
- if (where < (int) strlen(sptr))
- strncpy(tmp.sptr,sptr+where,len);
- return tmp;
- }
-
- // ---- find offset to first instance of specified char
- int pString::FindChar(unsigned char ch)
- {
- char *cp = strchr(sptr, ch);
- if (cp == NULL)
- return -1;
- return (int) (cp - sptr);
- }
-
- // ------- stream I/O
- ostream& operator<< (ostream& os, pString& str)
- {
- os << str.sptr;
- return os;
- }
-
- istream& operator>> (istream& is, pString& str)
- {
- *str.sptr = '\0';
- while (*str.sptr == '\0')
- is.getline(str.sptr, str.length+1);
- return is;
- }
-
-